package cn.edu.dhu.acm.oj.client.panel;

import java.io.*;
import java.awt.print.*;
import cn.edu.dhu.acm.oj.client.panel.ate.*;
import cn.edu.dhu.acm.oj.client.Control;
import cn.edu.dhu.acm.oj.common.config.Const;
import java.text.MessageFormat;

public class CodePanel extends javax.swing.JPanel {

	public CodePanel(String title, int codepanelcnt, javax.swing.JTabbedPane jtp) {
		initComponents();
		JEP_Code = new ClipTextArea(JSP_Code);
		this.JCB_Language.removeAllItems();
		for (int i = 0; i < Const.LANGUAGE.length; i++) {
			JCB_Language.addItem(Const.LANGUAGE[i]);
		}
		language = Control.getLanguage();
		JCB_Language.setSelectedItem(language);
		name = title;
		panelint = codepanelcnt;
		tabbedPane = jtp;
		JSP_Code.setViewportView(JEP_Code);
		file = new File(Control.getWorkpath() + title);
		line = System.getProperty("line.separator");
		JEP_Code.setText("//Code here");
	}

	public void setCode(String str) {
		JEP_Code.setText(str);
	}

	public void Save() throws IOException {
		BufferedWriter out = new BufferedWriter(new FileWriter(file));
		out.write(JEP_Code.getText());
		out.close();
	}

	/** This method is called from within the constructor to
	 * initialize the form.
	 * WARNING: Do NOT modify this code. The content of this method is
	 * always regenerated by the Form Editor.
	 */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        TB_Code = new javax.swing.JToolBar();
        JCB_Language = new javax.swing.JComboBox();
        JB_New = new javax.swing.JButton();
        JB_Open = new javax.swing.JButton();
        JB_Save = new javax.swing.JButton();
        JB_Compile = new javax.swing.JButton();
        JB_Test = new javax.swing.JButton();
        JB_Submit = new javax.swing.JButton();
        JB_Print = new javax.swing.JButton();
        jSplitPane1 = new javax.swing.JSplitPane();
        JSP_Code = new javax.swing.JScrollPane();
        JEP_Tmep = new javax.swing.JEditorPane();
        JTP_Info = new javax.swing.JTabbedPane();
        JP_CompileInfo = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        JTA_CompileInfo = new javax.swing.JTextArea();
        JP_Test = new javax.swing.JPanel();
        TB_Test = new javax.swing.JToolBar();
        JB_RunTest = new javax.swing.JButton();
        JB_Clear = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        TF_Time = new javax.swing.JTextField();
        jPanelAll = new javax.swing.JPanel();
        JP_Left = new javax.swing.JPanel();
        jLabel2 = new javax.swing.JLabel();
        jScrollPane2 = new javax.swing.JScrollPane();
        JTA_In = new javax.swing.JTextArea();
        JP_Right = new javax.swing.JPanel();
        jScrollPane3 = new javax.swing.JScrollPane();
        JTA_Out = new javax.swing.JTextArea();
        jLabel3 = new javax.swing.JLabel();

        setLayout(new java.awt.BorderLayout());

        TB_Code.setFloatable(false);

        JCB_Language.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                JCB_LanguageActionPerformed(evt);
            }
        });
        TB_Code.add(JCB_Language);

        JB_New.setText("New");
        JB_New.setToolTipText("New");
        JB_New.setFocusable(false);
        JB_New.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        JB_New.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        JB_New.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                JB_NewActionPerformed(evt);
            }
        });
        TB_Code.add(JB_New);

        JB_Open.setText("Open");
        JB_Open.setToolTipText("Open");
        JB_Open.setFocusable(false);
        JB_Open.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        JB_Open.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        JB_Open.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                JB_OpenActionPerformed(evt);
            }
        });
        TB_Code.add(JB_Open);

        JB_Save.setText("SaveAs");
        JB_Save.setToolTipText("SaveAs");
        JB_Save.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                JB_SaveActionPerformed(evt);
            }
        });
        TB_Code.add(JB_Save);

        JB_Compile.setText("Compile");
        JB_Compile.setToolTipText("Save+Compile");
        JB_Compile.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                JB_CompileActionPerformed(evt);
            }
        });
        TB_Code.add(JB_Compile);

        JB_Test.setText("Test");
        JB_Test.setToolTipText("To TestPanel");
        JB_Test.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                JB_TestActionPerformed(evt);
            }
        });
        TB_Code.add(JB_Test);

        JB_Submit.setText("Submit");
        JB_Submit.setToolTipText("Save+Run+Submit");
        JB_Submit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                JB_SubmitActionPerformed(evt);
            }
        });
        TB_Code.add(JB_Submit);

        JB_Print.setText("Print");
        JB_Print.setToolTipText("print");
        JB_Print.setFocusable(false);
        JB_Print.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        JB_Print.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        JB_Print.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                JB_PrintActionPerformed(evt);
            }
        });
        TB_Code.add(JB_Print);

        add(TB_Code, java.awt.BorderLayout.NORTH);

        jSplitPane1.setDividerLocation(300);
        jSplitPane1.setDividerSize(7);
        jSplitPane1.setOrientation(javax.swing.JSplitPane.VERTICAL_SPLIT);
        jSplitPane1.setOneTouchExpandable(true);

        JSP_Code.setViewportView(JEP_Tmep);

        jSplitPane1.setTopComponent(JSP_Code);

        JP_CompileInfo.setLayout(new java.awt.BorderLayout());

        JTA_CompileInfo.setColumns(20);
        JTA_CompileInfo.setRows(5);
        jScrollPane1.setViewportView(JTA_CompileInfo);

        JP_CompileInfo.add(jScrollPane1, java.awt.BorderLayout.CENTER);

        JTP_Info.addTab("CompileInformation", JP_CompileInfo);

        JP_Test.setLayout(new java.awt.BorderLayout());

        TB_Test.setFloatable(false);

        JB_RunTest.setText("Compile&RunTest");
        JB_RunTest.setToolTipText("TestIn'll endwith \\n");
        JB_RunTest.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                JB_RunTestActionPerformed(evt);
            }
        });
        TB_Test.add(JB_RunTest);

        JB_Clear.setText("Clear");
        JB_Clear.setToolTipText("Clear");
        JB_Clear.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                JB_ClearActionPerformed(evt);
            }
        });
        TB_Test.add(JB_Clear);

        jLabel1.setText("Time_Limit(sec) : ");
        TB_Test.add(jLabel1);

        TF_Time.setText("5");
        TF_Time.setToolTipText("Java'll *3 automatically");
        TB_Test.add(TF_Time);

        JP_Test.add(TB_Test, java.awt.BorderLayout.NORTH);

        jPanelAll.setLayout(new java.awt.GridLayout(1, 2));

        JP_Left.setLayout(new java.awt.BorderLayout());

        jLabel2.setText("Test In:");
        JP_Left.add(jLabel2, java.awt.BorderLayout.NORTH);

        JTA_In.setColumns(20);
        JTA_In.setFont(new java.awt.Font("DialogInput", 0, 13));
        JTA_In.setRows(5);
        jScrollPane2.setViewportView(JTA_In);

        JP_Left.add(jScrollPane2, java.awt.BorderLayout.CENTER);

        jPanelAll.add(JP_Left);

        JP_Right.setLayout(new java.awt.BorderLayout());

        JTA_Out.setColumns(20);
        JTA_Out.setFont(new java.awt.Font("DialogInput", 0, 13));
        JTA_Out.setRows(5);
        jScrollPane3.setViewportView(JTA_Out);

        JP_Right.add(jScrollPane3, java.awt.BorderLayout.CENTER);

        jLabel3.setText("RunTest Result:");
        JP_Right.add(jLabel3, java.awt.BorderLayout.NORTH);

        jPanelAll.add(JP_Right);

        JP_Test.add(jPanelAll, java.awt.BorderLayout.CENTER);

        JTP_Info.addTab("Test", JP_Test);

        jSplitPane1.setRightComponent(JTP_Info);

        add(jSplitPane1, java.awt.BorderLayout.CENTER);
    }// </editor-fold>//GEN-END:initComponents

private void JB_SaveActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_JB_SaveActionPerformed
	javax.swing.JFileChooser chooser = new javax.swing.JFileChooser(Control.getWorkpath());
	chooser.setSelectedFile(new File(name));
	if (chooser.showSaveDialog(this) == javax.swing.JFileChooser.APPROVE_OPTION) {
		try {
			file = chooser.getSelectedFile();
			name = file.getName();
			tabbedPane.setTitleAt(panelint, name);
			Save();
		} catch (Exception ex) {
			Control.getMainFrame().smallDialog("Save File Error!", "Error", 0);
		}
	}
}//GEN-LAST:event_JB_SaveActionPerformed

    private void JCB_LanguageActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_JCB_LanguageActionPerformed
		language = JCB_Language.getSelectedItem().toString();
		JEP_Code.setLanguage(language);
}//GEN-LAST:event_JCB_LanguageActionPerformed

private void JB_SubmitActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_JB_SubmitActionPerformed
	try {
		Save();
		Control.setLanguage(language);
		Control.setCode(JEP_Code.getText() + line);
		javax.swing.JDialog dialog = new javax.swing.JDialog();
		SubmitPanel submitpanel = new SubmitPanel(dialog);
		Control.getMainFrame().newDialog(dialog, submitpanel, "Submit");
	} catch (Exception e) {
		Control.getMainFrame().smallDialog("Save File Error!", "Error", 0);
	}
}//GEN-LAST:event_JB_SubmitActionPerformed

    private void JB_CompileActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_JB_CompileActionPerformed
		try {
			Save();
			JTA_CompileInfo.setText("Compiling...");
			Control.setLanguage(language);
			Control.setCode(JEP_Code.getText() + line);
			Control.Compile(true);
			JTA_CompileInfo.setText(Control.getCompileOut());
		} catch (Exception ioe) {
			Control.getMainFrame().smallDialog("Save File Error!", "Error", 0);
		}
    }//GEN-LAST:event_JB_CompileActionPerformed

    private void JB_TestActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_JB_TestActionPerformed
		JTP_Info.setSelectedIndex(1);
    }//GEN-LAST:event_JB_TestActionPerformed

    private void JB_NewActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_JB_NewActionPerformed
		Control.getMainFrame().NewCodePanel();
}//GEN-LAST:event_JB_NewActionPerformed

    private void JB_OpenActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_JB_OpenActionPerformed
		Control.getMainFrame().OpenCodePanel();
    }//GEN-LAST:event_JB_OpenActionPerformed

    private void JB_RunTestActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_JB_RunTestActionPerformed
		try {
			Save();
			JTA_CompileInfo.setText("Compiling~~~");
			Control.setLanguage(language);
			Control.setCode(JEP_Code.getText() + line);
			long tl = Long.parseLong(TF_Time.getText());
			String testin = JTA_In.getText();
			if (!testin.endsWith("\n")) {
				testin += "\n";
				JTA_In.append("\n");
			}
			Control.RunTest(testin, tl);

			JTA_CompileInfo.setText(Control.getCompileOut());
			JTA_Out.setText(Control.getTestOut());
		} catch (IOException ioe) {
			Control.getMainFrame().smallDialog("Save File Error!", "Error", 0);
		} catch (NumberFormatException ne) {
			JTA_Out.setText("TimeLimit Error!\nIt isn't a number!");
		} catch (Exception e) {
			e.printStackTrace();
			JTA_CompileInfo.setText("Set your Compile Path!");
		}

}//GEN-LAST:event_JB_RunTestActionPerformed

    private void JB_ClearActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_JB_ClearActionPerformed
		JTA_In.setText("");
		JTA_Out.setText("");
}//GEN-LAST:event_JB_ClearActionPerformed

    private void JB_PrintActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_JB_PrintActionPerformed
		MessageFormat Fmt = new MessageFormat(Control.getUserID());
		javax.swing.JTextArea tmp =new javax.swing.JTextArea();
		tmp.setTabSize(4);
		tmp.setFont(new java.awt.Font("Courier New", 0, 7));
		tmp.setText(JEP_Code.getText());
		try {
			boolean status = tmp.print(Fmt, null);
			if (status) {
				Control.getMainFrame().smallDialog("Print OK!", "OK", 0);
			} else {
				Control.getMainFrame().smallDialog("Print Error!", "Error", 0);
			}
		} catch (Exception pe) {
			Control.getMainFrame().smallDialog("Print Error!", "Error", 0);
		}
		tmp = null;
}//GEN-LAST:event_JB_PrintActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton JB_Clear;
    private javax.swing.JButton JB_Compile;
    private javax.swing.JButton JB_New;
    private javax.swing.JButton JB_Open;
    private javax.swing.JButton JB_Print;
    private javax.swing.JButton JB_RunTest;
    private javax.swing.JButton JB_Save;
    private javax.swing.JButton JB_Submit;
    private javax.swing.JButton JB_Test;
    private javax.swing.JComboBox JCB_Language;
    private javax.swing.JEditorPane JEP_Tmep;
    private javax.swing.JPanel JP_CompileInfo;
    private javax.swing.JPanel JP_Left;
    private javax.swing.JPanel JP_Right;
    private javax.swing.JPanel JP_Test;
    private javax.swing.JScrollPane JSP_Code;
    private javax.swing.JTextArea JTA_CompileInfo;
    private javax.swing.JTextArea JTA_In;
    private javax.swing.JTextArea JTA_Out;
    private javax.swing.JTabbedPane JTP_Info;
    private javax.swing.JToolBar TB_Code;
    private javax.swing.JToolBar TB_Test;
    private javax.swing.JTextField TF_Time;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JPanel jPanelAll;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JSplitPane jSplitPane1;
    // End of variables declaration//GEN-END:variables
	private String language = "CPP";
	private String name;
	private int panelint;
	private javax.swing.JTabbedPane tabbedPane;
	private ClipTextArea JEP_Code;
	private File file;
	private String line;
}
